/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.servlets;

import com.myapp.struts.beans.ScheduleBean;
import com.myapp.struts.dao.Connect;
import com.myapp.struts.dao.ScheduleDao;
import java.io.IOException;
import java.io.PrintWriter;


import java.util.*;

import javax.servlet.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ScheduleInsert extends HttpServlet {

    private static final long serialVersionUID = 1L;
    // instantiating Driver class
    public Connect drv = new Connect();
    // instance variable declaration
    public ArrayList al, alt;
    boolean bool;
    String str;
    private RequestDispatcher rd;

    // method that contains business logic
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // HttpServletRequestFacade rf=   (HttpServletRequestFacade)request;
        System.out.println(" INSIDE ScheduleInsert..........");
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        ServletContext application;

        // get the values from jsp
        String scheduleno = request.getParameter("scheduleno");
        String schedulename = request.getParameter("schedulename");
        String subschedule1 = request.getParameter("sub1");
        String subschedule2 = request.getParameter("sub2");       
        String description = request.getParameter("desc");
        java.sql.Date myDate = new java.sql.Date(System.currentTimeMillis());
        String date = myDate.toString();

        System.out.println("date......" + date);

        try {
            System.out.println("inside try in ScheduleInsert..");

            ScheduleBean shb = new ScheduleBean();

            shb.setSchedule_No(scheduleno);
            shb.setSchedule_Name(schedulename);
            shb.setSub_Schedule1(subschedule1);
            shb.setSub_Schedule2(subschedule2);           
            shb.setDescription(description);
            shb.setCreatedBy("admin");
            shb.setCreatedDt(date);
            shb.setSchedule_Status("1");

            // Instantiating insert DAO at scheduleInserServ Servlet
            ScheduleDao shd = new ScheduleDao();

            bool = shd.insert(shb);
            System.out.println("Boolean" + bool);
            application = getServletContext();
            System.out.println("before if...");

            if (bool == true) {
                rd = request.getRequestDispatcher("com/myapp/home/success.jsp?code=114");
                rd.forward(request, response);
            } else {
                rd = request.getRequestDispatcher("com/myapp/home/fail.jsp?code=114");
                rd.forward(request, response);
            }
        } // end of try
        catch (Exception e) {
            System.out.println("CAUGHT AT ScheduleInsert SERVLET" + e);
        } // end of catch
        finally {
            if (drv.mySql() != null) {
                try {
                    drv.mySql().close();
                    System.out.println("DATABASE CONNECTION TERMINATED");
                } // end of try
                catch (Exception e) {    /* ignore close errors */

                }                                // catch block close
            }                                    // end of if
        }                                        // end of finally
    }                                            // End of service method
}    // End of class

